package com.cloud.cop.validation;

import java.util.Locale;

import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.util.Assert;

public class MainErrors {

	public static final String ERROR_CODE_PREFIX = "ERROR_";
	public static final String ERROR_SOLUTION_SUBFIX = "_SOLUTION";
	
	//错误信息资源
	private static MessageSourceAccessor messageSourceAccessor;

	public static MainError getErrors(MainErrorType mainErrorType,Locale locale){
		String errorMessage = getErrorMessage(ERROR_CODE_PREFIX+mainErrorType.value(), locale);
		String solutionMessage = getErrorSolution(ERROR_CODE_PREFIX+mainErrorType.value()+ERROR_SOLUTION_SUBFIX, locale);
		return new DefaultMainError(mainErrorType.value(),errorMessage,solutionMessage);
	}
	
	public static String getErrorMessage(String code,Locale locale){
		Assert.notNull(messageSourceAccessor, "请先设置错误消息的国际化资源");
		return messageSourceAccessor.getMessage(code, new Object[]{}, locale);
	}
	
	public static String getErrorSolution(String code,Locale locale){
		Assert.notNull(messageSourceAccessor, "请先设置解决消息的国际化资源");
		return messageSourceAccessor.getMessage(code, new Object[]{}, locale);
	}
	
	public static void setMessageSourceAccessor(
			MessageSourceAccessor messageSourceAccessor) {
		MainErrors.messageSourceAccessor = messageSourceAccessor;
	}
	
}
